using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Autofac;
using Catastrophe.Common.AttributeEx;
using Catastrophe.Common.ObjM;
using Catastrophe.Initialize.IOCProvier;

namespace Catastrophe.Initialize
{
	public class CatastropheInitializer
	{
		public static string CatastropheBaseWfAssemblyName;

		public static List<Assembly> AssemblyList = new List<Assembly>();

		public static Dictionary<string, Assembly> FrameAssembly;

		public static Dictionary<string, List<IFileIni>> DicFileIni = new Dictionary<string, List<IFileIni>>();

		public static Dictionary<string, IContainerIni> DicContainerIni;

		public static Dictionary<string, List<IDbIni>> DicDbIni = new Dictionary<string, List<IDbIni>>();

		public static Dictionary<string, List<IAuthorizeIni>> DicAutorizeIni = new Dictionary<string, List<IAuthorizeIni>>();

		public static Dictionary<string, List<ICommIni>> DicCommIni = new Dictionary<string, List<ICommIni>>();

		public static Dictionary<string, List<ICommTaskIni>> DicCommTaskIni = new Dictionary<string, List<ICommTaskIni>>();

		public static Dictionary<string, List<IControlTaskIni>> DicControlTaskIni = new Dictionary<string, List<IControlTaskIni>>();

		public static Dictionary<string, List<IServiceIni>> DicServiceIni = new Dictionary<string, List<IServiceIni>>();

		public static List<string> AssemblyStrAry { get; set; }

		public static List<string> AssemblyStrModuleAry { get; set; }

		public static Action<object[]> FrameLoadFinishedHandler { get; set; }

		public static void AddIRunManager<T>(string key, T irun) where T : IRunManager
		{
			IFileIni fileIni = irun as IFileIni;
			if (fileIni != null)
			{
				if (!CatastropheInitializer.DicFileIni.ContainsKey(key))
				{
					CatastropheInitializer.DicFileIni.Add(key, new List<IFileIni>());
				}
				CatastropheInitializer.DicFileIni[key].Add(fileIni);
				return;
			}
			IDbIni dbIni = irun as IDbIni;
			if (dbIni != null)
			{
				if (!CatastropheInitializer.DicDbIni.ContainsKey(key))
				{
					CatastropheInitializer.DicDbIni.Add(key, new List<IDbIni>());
				}
				CatastropheInitializer.DicDbIni[key].Add(dbIni);
				return;
			}
			ICommIni commIni = irun as ICommIni;
			if (commIni != null)
			{
				if (!CatastropheInitializer.DicCommIni.ContainsKey(key))
				{
					CatastropheInitializer.DicCommIni.Add(key, new List<ICommIni>());
				}
				CatastropheInitializer.DicCommIni[key].Add(commIni);
				return;
			}
			ICommTaskIni commTaskIni = irun as ICommTaskIni;
			if (commTaskIni != null)
			{
				if (!CatastropheInitializer.DicCommTaskIni.ContainsKey(key))
				{
					CatastropheInitializer.DicCommTaskIni.Add(key, new List<ICommTaskIni>());
				}
				CatastropheInitializer.DicCommTaskIni[key].Add(commTaskIni);
				return;
			}
			IControlTaskIni controlTaskIni = irun as IControlTaskIni;
			if (controlTaskIni != null)
			{
				if (!CatastropheInitializer.DicControlTaskIni.ContainsKey(key))
				{
					CatastropheInitializer.DicControlTaskIni.Add(key, new List<IControlTaskIni>());
				}
				CatastropheInitializer.DicControlTaskIni[key].Add(controlTaskIni);
				return;
			}
			IServiceIni serviceIni = irun as IServiceIni;
			if (serviceIni != null)
			{
				if (!CatastropheInitializer.DicServiceIni.ContainsKey(key))
				{
					CatastropheInitializer.DicServiceIni.Add(key, new List<IServiceIni>());
				}
				CatastropheInitializer.DicServiceIni[key].Add(serviceIni);
				return;
			}
			IAuthorizeIni authorizeIni = irun as IAuthorizeIni;
			if (authorizeIni != null)
			{
				if (!CatastropheInitializer.DicAutorizeIni.ContainsKey(key))
				{
					CatastropheInitializer.DicAutorizeIni.Add(key, new List<IAuthorizeIni>());
				}
				CatastropheInitializer.DicAutorizeIni[key].Add(authorizeIni);
			}
		}

		[Obsolete("use CatastropheProvider<TInterface>.Get<T>() instead!")]
		public static IFileIni GetFileIniObj<T>()
		{
			if (CatastropheInitializer.DicFileIni == null)
			{
				return null;
			}
			string name = typeof(T).Assembly.GetName().Name;
			if (!string.IsNullOrEmpty(name) && CatastropheInitializer.DicFileIni.ContainsKey(name))
			{
				return CatastropheInitializer.DicFileIni[name].FirstOrDefault((IFileIni x) => x.GetType() == typeof(T));
			}
			return null;
		}

		[Obsolete("use CatastropheProvider<TInterface>.Get<T>() instead!")]
		public static IDbIni GetDbIniObj<T>()
		{
			if (CatastropheInitializer.DicDbIni == null)
			{
				return null;
			}
			string name = typeof(T).Assembly.GetName().Name;
			if (!string.IsNullOrEmpty(name) && CatastropheInitializer.DicDbIni.ContainsKey(name))
			{
				return CatastropheInitializer.DicDbIni[name].FirstOrDefault((IDbIni x) => x.GetType() == typeof(T));
			}
			return null;
		}

		[Obsolete("use CatastropheProvider<TInterface>.Get<T>() instead!")]
		public static IAuthorizeIni GetUserPowerIniObj<T>()
		{
			if (CatastropheInitializer.DicAutorizeIni == null)
			{
				return null;
			}
			string name = typeof(T).Assembly.GetName().Name;
			if (!string.IsNullOrEmpty(name) && CatastropheInitializer.DicAutorizeIni.ContainsKey(name))
			{
				return CatastropheInitializer.DicAutorizeIni[name].FirstOrDefault((IAuthorizeIni x) => x.GetType() == typeof(T));
			}
			return null;
		}

		[Obsolete("use CatastropheProvider<TInterface>.Get<T>() instead!")]
		public static ICommIni GetCommIniObj<T>()
		{
			if (CatastropheInitializer.DicCommIni == null)
			{
				return null;
			}
			string name = typeof(T).Assembly.GetName().Name;
			if (!string.IsNullOrEmpty(name) && CatastropheInitializer.DicCommIni.ContainsKey(name))
			{
				return CatastropheInitializer.DicCommIni[name].FirstOrDefault((ICommIni x) => x.GetType() == typeof(T));
			}
			return null;
		}

		[Obsolete("use CatastropheProvider<TInterface>.Get<T>() instead!")]
		public static ICommTaskIni GetCommTaskIniObj<T>()
		{
			if (CatastropheInitializer.DicCommTaskIni == null)
			{
				return null;
			}
			string name = typeof(T).Assembly.GetName().Name;
			if (!string.IsNullOrEmpty(name) && CatastropheInitializer.DicCommTaskIni.ContainsKey(name))
			{
				return CatastropheInitializer.DicCommTaskIni[name].FirstOrDefault((ICommTaskIni x) => x.GetType() == typeof(T));
			}
			return null;
		}

		[Obsolete("use CatastropheProvider<TInterface>.Get<T>() instead!")]
		public static IControlTaskIni GetControlTaskIniObj<T>()
		{
			if (CatastropheInitializer.DicControlTaskIni == null)
			{
				return null;
			}
			string name = typeof(T).Assembly.GetName().Name;
			if (!string.IsNullOrEmpty(name) && CatastropheInitializer.DicControlTaskIni.ContainsKey(name))
			{
				return CatastropheInitializer.DicControlTaskIni[name].FirstOrDefault((IControlTaskIni x) => x.GetType() == typeof(T));
			}
			return null;
		}

		[SystemIni(SystemIniTypeEnum.Other, false)]
		public static void FrameAssemblyLoad()
		{
			if (CatastropheInitializer.AssemblyStrModuleAry == null)
			{
				throw new Exception("load CatastropheConfig.config failed!");
			}
			CatastropheInitializer.FrameAssembly = ReflectionUtil.GetAssembly(CatastropheInitializer.AssemblyStrModuleAry.ToArray());
			CatastropheInitializer.AssemblyStrAry = CatastropheInitializer.FrameAssembly.Keys.ToList();
			CatastropheInitializer.AssemblyList = CatastropheInitializer.FrameAssembly.Values.ToList();
		}

		[Obsolete("use ioc mode!")]
		[SystemIni(SystemIniTypeEnum.FileConfig, false)]
		public static void ExcuteFIleIni()
		{
			CatastropheInitializer.DicFileIni = ((CatastropheInitializer.AssemblyStrAry.Count == 0) ? ReflectionUtil.GetInterfaceDicMutile2<IFileIni>() : ReflectionUtil.GetInterfaceDicMutile2<IFileIni>(CatastropheInitializer.AssemblyList));
			ContainerBuilder containerBuilder = new ContainerBuilder();
			foreach (List<IFileIni> value in CatastropheInitializer.DicFileIni.Values)
			{
				foreach (IFileIni item in value)
				{
					containerBuilder.RegisterInstance(item);
					if (item.IsRun)
					{
						item.FileLoad();
					}
				}
			}
			CatastropheProvider<IFileIni>.Register(containerBuilder.Build());
		}

		[Obsolete("use ioc mode!")]
		[SystemIni(SystemIniTypeEnum.DbConfig, false)]
		public static void ExcuteDbIni()
		{
			CatastropheInitializer.DicDbIni = ((CatastropheInitializer.AssemblyStrAry.Count == 0) ? ReflectionUtil.GetInterfaceDicMutile2<IDbIni>() : ReflectionUtil.GetInterfaceDicMutile2<IDbIni>(CatastropheInitializer.AssemblyList));
			ContainerBuilder containerBuilder = new ContainerBuilder();
			foreach (List<IDbIni> value in CatastropheInitializer.DicDbIni.Values)
			{
				foreach (IDbIni item in value)
				{
					containerBuilder.RegisterInstance(item);
					if (item.IsRun)
					{
						item.DbLoad();
					}
				}
			}
			CatastropheProvider<IDbIni>.Register(containerBuilder.Build());
		}

		[SystemIni(SystemIniTypeEnum.DbConfig, false)]
		public static void ExcuteContainerIni()
		{
			CatastropheInitializer.DicContainerIni = ((CatastropheInitializer.AssemblyStrAry.Count == 0) ? ReflectionUtil.GetInterfaceDicMutile<IContainerIni>() : ReflectionUtil.GetInterfaceDicMutile<IContainerIni>(CatastropheInitializer.AssemblyList));
			ContainerBuilder containerBuilder = new ContainerBuilder();
			foreach (IContainerIni value in CatastropheInitializer.DicContainerIni.Values)
			{
				containerBuilder.RegisterInstance(value);
				ContainerBuilder containerBuilder2 = new ContainerBuilder();
				if (value.IsRun)
				{
					value.ContainerLoad(containerBuilder2);
					string name = value.GetType().Assembly.GetName().Name;
					CatastropheProvider.RegisterByKey(name, containerBuilder2.Build());
				}
			}
			CatastropheProvider<IContainerIni>.Register(containerBuilder.Build());
		}

		[Obsolete("use ioc mode!")]
		[SystemIni(SystemIniTypeEnum.Authority, false)]
		public static void ExcuteAuthorizeIni()
		{
			CatastropheInitializer.DicAutorizeIni = ((CatastropheInitializer.AssemblyStrAry.Count == 0) ? ReflectionUtil.GetInterfaceDicMutile2<IAuthorizeIni>() : ReflectionUtil.GetInterfaceDicMutile2<IAuthorizeIni>(CatastropheInitializer.AssemblyList));
			ContainerBuilder containerBuilder = new ContainerBuilder();
			foreach (List<IAuthorizeIni> value in CatastropheInitializer.DicAutorizeIni.Values)
			{
				foreach (IAuthorizeIni item in value)
				{
					containerBuilder.RegisterInstance(item);
					if (item.IsRun)
					{
						item.AuthorizeLoad();
					}
				}
			}
			CatastropheProvider<IAuthorizeIni>.Register(containerBuilder.Build());
		}

		[Obsolete("use ioc mode!")]
		[SystemIni(SystemIniTypeEnum.CommConfig, false)]
		public static void ExcuteComIni()
		{
			CatastropheInitializer.DicCommIni = ((CatastropheInitializer.AssemblyStrAry.Count == 0) ? ReflectionUtil.GetInterfaceDicMutile2<ICommIni>() : ReflectionUtil.GetInterfaceDicMutile2<ICommIni>(CatastropheInitializer.AssemblyList));
			ContainerBuilder containerBuilder = new ContainerBuilder();
			foreach (List<ICommIni> value in CatastropheInitializer.DicCommIni.Values)
			{
				foreach (ICommIni item in value)
				{
					containerBuilder.RegisterInstance(item);
					if (item.IsRun)
					{
						item.CommLoad();
					}
				}
			}
			CatastropheProvider<ICommIni>.Register(containerBuilder.Build());
		}

		[Obsolete("use ioc mode!")]
		[SystemIni(SystemIniTypeEnum.CommTask, false)]
		public static void ExcuteComTaskIni()
		{
			CatastropheInitializer.DicCommTaskIni = ((CatastropheInitializer.AssemblyStrAry.Count == 0) ? ReflectionUtil.GetInterfaceDicMutile2<ICommTaskIni>() : ReflectionUtil.GetInterfaceDicMutile2<ICommTaskIni>(CatastropheInitializer.AssemblyList));
			ContainerBuilder containerBuilder = new ContainerBuilder();
			foreach (List<ICommTaskIni> value in CatastropheInitializer.DicCommTaskIni.Values)
			{
				foreach (ICommTaskIni item in value)
				{
					containerBuilder.RegisterInstance(item);
					if (item.IsRun)
					{
						item.CommTaskLoad();
					}
				}
			}
			CatastropheProvider<ICommTaskIni>.Register(containerBuilder.Build());
		}

		[Obsolete("use ioc mode!")]
		[SystemIni(SystemIniTypeEnum.ControlTask, false)]
		public static void ExcuteControlTaskIni()
		{
			CatastropheInitializer.DicControlTaskIni = ((CatastropheInitializer.AssemblyStrAry.Count == 0) ? ReflectionUtil.GetInterfaceDicMutile2<IControlTaskIni>() : ReflectionUtil.GetInterfaceDicMutile2<IControlTaskIni>(CatastropheInitializer.AssemblyList));
			ContainerBuilder containerBuilder = new ContainerBuilder();
			foreach (List<IControlTaskIni> value in CatastropheInitializer.DicControlTaskIni.Values)
			{
				foreach (IControlTaskIni item in value)
				{
					containerBuilder.RegisterInstance(item);
					if (item.IsRun)
					{
						item.ControlTaskLoad();
					}
				}
			}
			CatastropheProvider<IControlTaskIni>.Register(containerBuilder.Build());
		}

		[Obsolete("use ioc mode!")]
		[SystemIni(SystemIniTypeEnum.Services, false)]
		public static void ExcuteServiceIni()
		{
			CatastropheInitializer.DicServiceIni = ((CatastropheInitializer.AssemblyStrAry.Count == 0) ? ReflectionUtil.GetInterfaceDicMutile2<IServiceIni>() : ReflectionUtil.GetInterfaceDicMutile2<IServiceIni>(CatastropheInitializer.AssemblyList));
			ContainerBuilder containerBuilder = new ContainerBuilder();
			foreach (List<IServiceIni> value in CatastropheInitializer.DicServiceIni.Values)
			{
				foreach (IServiceIni item in value)
				{
					containerBuilder.RegisterInstance(item);
					if (item.IsRun)
					{
						item.ServiceLoad();
					}
				}
			}
			CatastropheProvider<IServiceIni>.Register(containerBuilder.Build());
		}

		public static void FrameLoadIni(Assembly assembly)
		{
			try
			{
				string currentDirectory = Environment.CurrentDirectory;
				FrameEnvironment.CurrentAssembly = assembly;
				FrameEnvironment.MainAssemblyName = assembly.GetName().Name;
				int num = currentDirectory.LastIndexOf("\\" + FrameEnvironment.MainAssemblyName + "\\");
				if (num > 0)
				{
					string text = currentDirectory.Substring(0, num);
					if (Directory.Exists(text))
					{
						FrameEnvironment.LastRootPath = text;
					}
				}
			}
			catch (Exception)
			{
				throw;
			}
		}

		public static bool TryGetAssembly(string assemblyName, out Assembly assembly)
		{
			if (CatastropheInitializer.FrameAssembly.ContainsKey(assemblyName))
			{
				assembly = CatastropheInitializer.FrameAssembly[assemblyName];
				return true;
			}
			assembly = null;
			return false;
		}

		public static void RemoveDevAssembly(string devAssemblyName = "Catastrophe.DevAuto.Wpf")
		{
			if (CatastropheInitializer.AssemblyStrAry.Contains(devAssemblyName))
			{
				CatastropheInitializer.AssemblyStrAry.Remove(devAssemblyName);
				Assembly assembly = CatastropheInitializer.AssemblyList.FirstOrDefault((Assembly x) => x.GetName().Name == devAssemblyName);
				if (assembly != null)
				{
					CatastropheInitializer.AssemblyList.Remove(assembly);
				}
				CatastropheInitializer.FrameAssembly?.Remove(devAssemblyName);
			}
		}

		public static Dictionary<string, List<T>> GetAssemblyInstance<T>()
		{
			Dictionary<string, List<T>> dictionary = new Dictionary<string, List<T>>();
			if (CatastropheInitializer.AssemblyStrAry == null)
			{
				return dictionary;
			}
			foreach (string item in CatastropheInitializer.AssemblyStrAry)
			{
				IContainer iContainer = CatastropheProvider.GetIContainer(item);
				if (iContainer != null && iContainer.IsRegistered<T>())
				{
					List<T> list = iContainer.Resolve<IEnumerable<T>>().ToList();
					if (list.Count > 0)
					{
						dictionary.Add(item, list.ToList());
					}
				}
			}
			return dictionary;
		}

		public static List<T> GetAssemblyInstanceList<T>()
		{
			List<T> list = new List<T>();
			if (CatastropheInitializer.AssemblyStrAry == null)
			{
				return list;
			}
			foreach (string item in CatastropheInitializer.AssemblyStrAry)
			{
				IContainer iContainer = CatastropheProvider.GetIContainer(item);
				if (iContainer != null && iContainer.IsRegistered<T>())
				{
					IEnumerable<T> enumerable = iContainer.Resolve<IEnumerable<T>>();
					if (enumerable.Count() > 0)
					{
						list.AddRange(enumerable);
					}
				}
			}
			return list;
		}

		public static List<T> GetAssemblyInstance<T>(string assembly)
		{
			List<T> result = new List<T>();
			if (CatastropheInitializer.AssemblyStrAry == null)
			{
				return result;
			}
			IContainer iContainer = CatastropheProvider.GetIContainer(assembly);
			if (iContainer == null)
			{
				return result;
			}
			if (!iContainer.IsRegistered<T>())
			{
				return result;
			}
			return iContainer.Resolve<IEnumerable<T>>().ToList();
		}
	}
}
